<?php


namespace App\Admin\Controllers\Config;

use App\Admin\Actions\SubTableAction;
use App\Models\Config\SystemDict;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Show;
use App\Admin\Renderable\DictDatumTable;
use Dcat\Admin\Http\Controllers\AdminController;

class DictController extends AdminController
{
    /**
     * @var $repository EloquentRepository
     */
    protected $repository;

    public function __construct()
    {
        $this->repository = new EloquentRepository(SystemDict::class);

        Admin::translator()->setPath("dict");
    }

    public function title()
    {
        return admin_trans_label("dict");
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make($this->repository, function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('dict_name');
            $grid->column('dict_key');
            $grid->column('values')->display('查看')->width(150)->modal( function (Grid\Displayers\Modal $modal){
                $modal->title('查看字典值');
                $modal->icon('feather icon-eye');
                $table = DictDatumTable::make(["dict_id"=>$this->getKey()]);
                return $table;
            });
            $grid->column('state')->switch()->width(100);
            $grid->column('remark')->limit(20)->width(250);;
            $grid->column('created_at')->width(170);
            $grid->column('updated_at')->sortable()->width(170);


            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->prepend(new SubTableAction("编辑字典","dict-datum?dict_id=".$actions->getKey()));
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                //如果存在条件就展开搜索框，否则点击才会显示
                if(!empty(request()->all())){
                    $filter->expand(true);
                }

                $filter->like('dict_name')->width(2);
                $filter->like('dict_key')->width(2);
            });

            $grid->enableDialogCreate();
            $grid->showQuickEditButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make($this->repository, function (Form $form) {
            $form->display('id');
            $form->text('dict_name')->required();
            $form->text('dict_key')->required();
            $form->radio('state')->required()->default(1)->options(['0' => '关闭', '1' => '开启']);
            $form->textarea('remark');
        });
    }
}
